﻿using IdentityModel;
using IdentityServer4.Models;

namespace Identity.Api.Configuration
{
    public class Config
    {

        public static IEnumerable<IdentityResource> GetResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Phone(),
                new IdentityResource("users", "用户", new List<string> { JwtClaimTypes.PreferredUserName }),
                new IdentityResource("username", "用户名", new List<string> { "username" }),
            };
        }


        public static IEnumerable<ApiScope> GetApiScopes()
        {
            return new ApiScope[] {
                 new ApiScope("product","商品"),
            };
        }

        public static IEnumerable<ApiResource> GetApis()
        {
            return new List<ApiResource>
            {
                new ApiResource("product", "Product Service"){

                 Scopes={ "product"},
                },
                new ApiResource("product1", "Product Service"){

                    UserClaims = { JwtClaimTypes.Name, JwtClaimTypes.PreferredUserName,"username" },

                    Scopes={"product"},

                    ApiSecrets = new List<Secret>()
                    {
                        new Secret("api_secret".Sha256())
                    },
                },
            };
        }


        // client want to access resources (aka scopes)
        public static IEnumerable<Client> GetClients(Dictionary<string, string> clientsUrl)
        {
            var clientHost = clientsUrl["ProductApi"];
            return new List<Client>
            {
                //new Client
                //{
                //    ClientId = "Product",
                //    ClientName = "Product Client",
                //    ClientSecrets = new List<Secret>
                //    {
                //        new Secret("secret".Sha256())
                //    },
                //    ClientUri = $"{clientsUrl["ProductApi"]}",                             // public uri of the client
                //    AllowedGrantTypes = GrantTypes.Hybrid,
                //    AllowAccessTokensViaBrowser = true,
                //    RequireConsent = false,
                //    AllowOfflineAccess = true,
                //    RedirectUris = new List<string>
                //    {
                //        $"{clientsUrl["ProductApi"]}/signin-oidc"
                //    },
                //    PostLogoutRedirectUris = new List<string>
                //    {
                //        $"{clientsUrl["ProductApi"]}/signout-callback-oidc"
                //    },
                //    AllowedScopes = new List<string>
                //    {
                //        IdentityServerConstants.StandardScopes.OpenId,
                //        IdentityServerConstants.StandardScopes.Profile,
                //        IdentityServerConstants.StandardScopes.OfflineAccess,
                //        "product"
                //    }
                //}
                //  },
                new Client
                {
                    ClientId = "productswaggerui",
                    ClientName = "Product Swagger UI",
                    AllowedGrantTypes = GrantTypes.Implicit,
                    RedirectUris = { $"{clientHost}/swagger/oauth2-redirect.html" },
                    AllowAccessTokensViaBrowser = true,
                    PostLogoutRedirectUris = { $"{clientsUrl["ProductApi"]}/oauth2-redirect.html" },
                    AllowedScopes =
                    {
                        "product"
                    }
                }
            };
        }
    }
}